/*
 * Processing.c
 *
 * Created: 18.02.2013 00:02:41
 *  Author: victor
 */
#include <Processing.h>
#include <Basetypes.h>
#include <CommonDefinitions.h>
#include <SerialHdl.h>
#include <OutputHdl.h>
#include <Serialdrv.h>

const u16 Processing_C_0x010F_Reply_au16[4] = {0x010F, 0x0048, 0x0001, COMMONDEFINITIONS_C_EOM_U16};
const u16 Processing_C_0x0107_1_Reply_au16[5] = {0x0107, 0x0003, 0x003D, 0x00B1, COMMONDEFINITIONS_C_EOM_U16};
const u16 Processing_C_0x0107_2_Reply_au16[5] = {0x0107, 0x0000, 0x0000, 0x0000, COMMONDEFINITIONS_C_EOM_U16};
const u16 Processing_C_0x010E_Reply_au16[4] = {0x010E, 0x0009, 0x0003, COMMONDEFINITIONS_C_EOM_U16};
const u16 Processing_C_0x0101_Reply_au16[4] = {0x0101, 0x0002, 0x0004, COMMONDEFINITIONS_C_EOM_U16};
const u16 Processing_C_0x010B_Reply_au16[10] = {0x010B, 0x0041, 0x006C, 0x0075, 0x0065 , 0x0026, 0x004D, 0x0065, 0x0031, COMMONDEFINITIONS_C_EOM_U16};
const u16 Processing_C_0x010D_Reply_au16[6] = {0x010D, 0x0003, 0x0004, 0x0005, 0x0006, COMMONDEFINITIONS_C_EOM_U16};
const u16 Processing_C_0x0103_0x0009_Reply_au16[6] = {0x0103, 0x0020, 0x0009, 0x0020, 0x0000, COMMONDEFINITIONS_C_EOM_U16};
const u16 Processing_C_0x0103_0x000A_Reply_au16[6] = {0x0103, 0x0020, 0x000A, 0x0020, 0x0000, COMMONDEFINITIONS_C_EOM_U16};
const u16 Processing_C_0x0103_0x000C_Reply_au16[6] = {0x0103, 0x0020, 0x000C, 0x0020, 0x0000, COMMONDEFINITIONS_C_EOM_U16};
	
static u16 Processing_min_ctr_u16 = ZERO;
static u16 Processing_sec_ctr_u8 = ZERO;

	
static u8 Processing_HuCommand_Flag_u8 = ZERO;
extern u8 OutputHdl_ClearToSendFLAG_u8;
/* Flag to indicate that the system is in playing mode */
static u8 Processing_Play_Flag=ZERO;

/* Local functions prototypes */
void Processing_AnswerAndCommandDecision ( u16* command );

void Processing_GetIncomingMessage ( u16 message )
{
	static u8 tmp_Frame_Index_u8 = ZERO;
	//vladvi1 static u8 tmp_MessageReceived_Flag_u8 = ZERO;
	//vladvi1 static u8 tmp_EchoReceived_Flag_u8 = ZERO;
	static u16 tmp_RecMessageBuffer_au16[11];
	//vladvi1 static u8 tmp_LengthOfRecMes_u8 = ZERO;
	
	/*Flag for Output HDL to know when it can send the next word from the buffer*/
	OutputHdl_ClearToSendFLAG_u8 = ONE;
		
	///////////vladvi1
	u8 tmp_delay_u8 = 90;
	
	while ( tmp_delay_u8 )
	{
		tmp_delay_u8--;
	}
	
	/////////vladvi1
	
	/* Check if a SOM or EOM was received they are the only frames that have the 9th bit set */
	if ( ( u16 ) ( message & 0x0100 ) )
	{
		/* Check if EOM was received, otherwise, it is a SOM */
		if ( message != COMMONDEFINITIONS_C_EOM_U16 )
		{
			/* Store the first received frame (SOF) */
			tmp_Frame_Index_u8 = ZERO;
			tmp_RecMessageBuffer_au16[tmp_Frame_Index_u8] = message;
			
			/* Check if it is the HU SOM or the CDC SOM echoed */
			switch ( message )
			{
				case COMMONDEFINITIONS_C_HU_SOM_U16:
					{
						/* Case it is a HU SOM, then signal that it is a HU command */
						Processing_HuCommand_Flag_u8 = ONE;
						/* Echo the frame */
						SerialHdl_SendData ( message );
						break;
					}
					
				default:
					{
						/* It is a SOM but not from HU, it is the echo of our sent frame, so we
						don't have to reply anything */
						Processing_HuCommand_Flag_u8 = ZERO;
						//vladvi1 tmp_EchoReceived_Flag_u8 = ONE;
						break;
					}
			}
			
		}
		
		else
		{
			/* If we received EOM we calculate the length of the message
			and call the function that decides what to do based on received message */
			tmp_Frame_Index_u8++;
			tmp_RecMessageBuffer_au16[tmp_Frame_Index_u8] = message;
			
			// vladvi1 tmp_LengthOfRecMes_u8 = tmp_Frame_Index_u8 + 1;
			//vladvi1 tmp_MessageReceived_Flag_u8 = ONE;
			if ( ONE == Processing_HuCommand_Flag_u8 )
			{
				Processing_AnswerAndCommandDecision ( tmp_RecMessageBuffer_au16 );
			}
			
		}
	}
	
	else
	{
		tmp_Frame_Index_u8++;
		tmp_RecMessageBuffer_au16[tmp_Frame_Index_u8] = message;
		
		/* Only if command was received we have to echo, otherwise, it is already an echo of what we've sent*/
		if ( ONE == Processing_HuCommand_Flag_u8 )
		{
			SerialHdl_SendData ( message );
		}
	}
}

void Processing_AnswerAndCommandDecision ( u16* command )
{
	static u8 tmp_PlayCmdFirstTime = ONE;
	static u8 tmp_PauseCmdFirstTime = ONE;
	
	switch ( command[1] )
	{
		case 0x00AD:/*Startup first message from HU */
			{
				/* Power ON the BTT here instead at 0x00A7 because the 5 seconds it needs to power up*/
				OutputHdl_Command ( POWER_ON_EN );
				break;
			}
			
		case 0x0048:
			{
				switch ( command[2] )
				{
					case  0x0001:
						{
							/* Send back confirmation for 9600bps*/
							OutputHdl_SerSendBackConfirmation ( Processing_C_0x010F_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x010F_Reply_au16 ) / 2 ) );
							break;
						}
						
					case 0x0002:
						{
							/* Set to double com speed 4800->9600bps*/
							SerialDrv_DoubleSpeed();
							/* No response needed */
							break;
						}
						
					default:
						{
							/* do nothing */
							break;
						}
				}
				
				break;
			}
			
		case 0x00B0:
			{
				/*send 0x0107 0x0003 0x003D 0x00B1 0x014F*/
				OutputHdl_SerSendBackConfirmation ( Processing_C_0x0107_1_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x0107_1_Reply_au16 ) / 2 ) );
				break;
			}
			
		case 0x00B3:
			{
				/* send 0x0107 0x0000 0x0000 0x0000 0x0147*/
				OutputHdl_SerSendBackConfirmation ( Processing_C_0x0107_2_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x0107_2_Reply_au16 ) / 2 ) );
				break;
			}
			
		case 0x00A7:
			{
				/* send {0x010E, 0x0009, 0x0003, 0x0147} */
				OutputHdl_SerSendBackConfirmation ( Processing_C_0x010E_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x010E_Reply_au16 ) / 2 ) );
				/* In order the BTT to power up earlier, the power on sequence moved at first received command */
				break;
			}
			
			/*Play command*/
		case 0x00A5:
			{
				/*send 0x0101 0x0002 0x0004 0x014F, CD#, track#*/
				OutputHdl_SerSendBackConfirmation ( Processing_C_0x0101_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x0101_Reply_au16 ) / 2 ) );
				
				/* TODO Maybe DELAY is required of 30 ms and check if no other HU command came in between those commands.
				if any command came, then the message below might not be send*/
				if ( ONE == tmp_PlayCmdFirstTime )
				{
					tmp_PlayCmdFirstTime = ZERO;
				}
				
				else
				{
					/*send 0x0103 0x0020 0x0009 0x0020 0x0000 0x014F, DMS info*/
					OutputHdl_SerSendBackConfirmation ( Processing_C_0x0103_0x0009_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x0103_0x0009_Reply_au16 ) / 2 ) );
					/*send {0x010B, 0x0041, 0x006C, 0x0075, 0x0065,0x0026, 0x004D, 0x0065, 0x0031, PROCESSING_C_EOM_U16}, Name of the CD Blue&Me1*/
					OutputHdl_SerSendBackConfirmation ( Processing_C_0x010B_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x010B_Reply_au16 ) / 2 ) );
					/*send  {0x0103, 0x0020, 0x0009, 0x0020, 0x0000, PROCESSING_C_EOM_U16}, TR#, TRGES, MIN, SEC*/
					OutputHdl_SerSendBackConfirmation ( Processing_C_0x010D_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x010D_Reply_au16 ) / 2 ) );
					/*Send command PLAY*/
					OutputHdl_Command ( PLAY_TRACK_EN );
					
					/* TODO send time message each second */
					/* Signal that the system is playing */
					Processing_Play_Flag = ONE;
				}
				
				break;
			}
			
			/* Pause command */
		case 0x0021:
			{
				/*send 0x0103 0x0020 0x000A 0x0020 0x0000 0x014F, DMS info*/
				OutputHdl_SerSendBackConfirmation ( Processing_C_0x0103_0x000A_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x0103_0x000A_Reply_au16 ) / 2 ) );
				
				/* TODO Maybe DELAY is required of 30 ms and check if no other HU command came in between those commands.
				if any command came, then the message below might not be send*/
				if ( ONE == tmp_PauseCmdFirstTime )
				{
					tmp_PauseCmdFirstTime = ZERO;
				}
				
				else
				{
					/*send 0x0103 0x0020 0x000C 0x0020 0x0000 0x014F, DMS info*/
					OutputHdl_SerSendBackConfirmation ( Processing_C_0x0103_0x000C_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x0103_0x000C_Reply_au16 ) / 2 ) );
					/*Send command PAUSE*/
					OutputHdl_Command ( PAUSE_TRACK_EN );
					/* Signal that the system is not playing */
					Processing_Play_Flag = ZERO;
				}
				
				break;
			}
			
			/*0x0180 0x0082 0x014F; 0x0180 0x0002 0x014F Next track*/
		case 0x0082:
			{
				/* set next_flag for the following command 0x0002*/
				break;
			}
			
			/*0x0180 0x0082 0x014F; 0x0180 0x0002 0x014F Next track*/
		case 0x0002:
			{
				/* check next_flag if 0x0082 was before then command NEXT track */
				/*send 0x0101 0x0001 0x0001 0x014F, CD#, track#*/
				OutputHdl_SerSendBackConfirmation ( Processing_C_0x0101_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x0101_Reply_au16 ) / 2 ) );
				/* Send NEXt command */
				OutputHdl_Command ( NEXT_TRACK_EN );
				break;
			}
			
			/*0x0180 0x0081 0x014F; 0x0180 0x0001 0x014F Previous track*/
		case 0x0081:
			{
				/* set back_flag for the following command 0x0001*/
				break;
			}
			
			/*0x0180 0x0081 0x014F; 0x0180 0x0001 0x014F Previous track*/
		case 0x0001:
			{
				/* check back_flag if 0x0081 was before then command Back track */
				/*send 0x0101 0x0001 0x0001 0x014F, CD#, track#*/
				OutputHdl_SerSendBackConfirmation ( Processing_C_0x0101_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x0101_Reply_au16 ) / 2 ) );
				/* Send Prev command */
				OutputHdl_Command ( PREVIOUS_TRACK_EN );
				break;
			}
			
			/*Press button for fast forward*/
		case 0x0091:
			{
				/* Send {0x0103, 0x0028, 0x0009, 0x0020, 0x0000, 0x014F} */
				OutputHdl_SerSendBackConfirmation ( Processing_C_0x0103_0x0009_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x0103_0x0009_Reply_au16 ) / 2 ) );
				/*press button for fast forward*/
				OutputHdl_Command ( FAST_FWD_EN );
				/* Dt betwen seconds decreases to 50ms and seconds increases */
				break;
			}
			
			/*Relase button for fast forward*/
		case 0x0011:
			{
				/* Send {0x0103, 0x0020, 0x0009, 0x0020, 0x0000, 0x014F} */
				OutputHdl_SerSendBackConfirmation ( Processing_C_0x0103_0x0009_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x0103_0x0009_Reply_au16 ) / 2 ) );
				/*Relase button for fast forward*/
				OutputHdl_Command ( FAST_FWD_EN );
				/* Dt betwen seconds increases to normal to 1s and seconds increases */
				break;
			}
			
			/*Press button for Rewind*/
		case 0x0092:
			{
				/* Send {0x0103, 0x0028, 0x0009, 0x0020, 0x0000, 0x014F} */
				OutputHdl_SerSendBackConfirmation ( Processing_C_0x0103_0x0009_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x0103_0x0009_Reply_au16 ) / 2 ) );
				/*press button for Rewind*/
				OutputHdl_Command ( REVERSE_EN );
				/* Dt betwen seconds decreases to 50ms and seconds decreases */
				break;
			}
			
			/*Relase button for Rewind*/
		case 0x0012:
			{
				/* Send {0x0103, 0x0020, 0x0009, 0x0020, 0x0000, 0x014F} */
				OutputHdl_SerSendBackConfirmation ( Processing_C_0x0103_0x0009_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x0103_0x0009_Reply_au16 ) / 2 ) );
				/*Relase button for Rewind*/
				OutputHdl_Command ( REVERSE_EN );
				/* Dt betwen seconds increases to normal to 1s and seconds increases */
				break;
			}
			/* Button Down Btt Restart part 1*/
		case 0x0083:
			{
				/* Wait for 0x0003 */
				/* Send BTT Reset text with 0x010b */
				OutputHdl_SerSendBackConfirmation ( Processing_C_0x010B_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x010B_Reply_au16 ) / 2 ) );
				break;
			}
			/* Button Down Btt Restart part 2*/
		case 0x0003:
			{
				/* Send BTT Rst text with 0x010b */
				OutputHdl_SerSendBackConfirmation ( Processing_C_0x010B_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x010B_Reply_au16 ) / 2 ) );
				/*Do reset the BTT by toggleing the transistor drain */
				OutputHdl_Command ( SUPPLY_RST_EN );
				
				//TODO a better method than waiting loop should be found
				
				///////////vladvi1
				u16 tmp_delay_u16 = 50000;
				
				while ( tmp_delay_u16 )
				{
					tmp_delay_u16--;
				}
				
				/////////vladvi1
				/* and then press the power button*/
				OutputHdl_Command ( POWER_ON_EN );
				break;
			}
			/* Button Up Phone answ part 1*/
		case 0x0084:
			{
				/* Wait for 0x0004 */
				/* Send stack rst text with 0x010b */
				OutputHdl_SerSendBackConfirmation ( Processing_C_0x010B_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x010B_Reply_au16 ) / 2 ) );
				break;
			}
			/* Button Up Phone answ part 2*/
		case 0x0004:
			{
				/* Send phone answer/hangup text with 0x010b */
				OutputHdl_SerSendBackConfirmation ( Processing_C_0x010B_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x010B_Reply_au16 ) / 2 ) );
				OutputHdl_Command ( PHONE_ANS_HUP_EN );
				/* Dt betwen seconds increases to normal to 1s and seconds increases */
				break;
			}
			
		default :
			{
				///* Set to double com speed 4800->9600bps because none of the recognized messages were received, so it maybe the case to listen @ higher speed */
				//SerialDrv_DoubleSpeed();
				break;
			}
	}
	
	/*command was evaluated and executed, so clear the command pending flag*/
	Processing_HuCommand_Flag_u8 = ZERO;
	
}


///* Function that will check periodicaly if power was not cut by
//comparing the PW_ON flag to NV_PW_ON flag.If they differ, the
//power was cut and the BTT has to be powered ON. */
///*  Instead of ciclicaly, this operation can be made only at Power on also.*/
void Processing_CyclicMessages ( )
{
	volatile u16 Processing_C_0x0109_Reply_au16[4] = {0x0109, Processing_min_ctr_u16, Processing_sec_ctr_u8, COMMONDEFINITIONS_C_EOM_U16};	
	if ( Processing_Play_Flag != ONE )
	{
		// do nothing
	}
	else
	{
		if (Processing_min_ctr_u16 <= 99)
		{
			if (Processing_sec_ctr_u8 == 60)
			{
				Processing_min_ctr_u16 ++;
				Processing_sec_ctr_u8 = 0;
			}
			else
			{
				Processing_sec_ctr_u8 ++;
			}
		}
		else
		{
			Processing_min_ctr_u16 = 0;
			Processing_sec_ctr_u8 = 0;
		}	
		
		/* send 0x0109 0x0000 0x0000 0x0000 0x0147*/
		OutputHdl_SerSendBackConfirmation ( Processing_C_0x0109_Reply_au16 , ( u8 ) ( ( sizeof Processing_C_0x0109_Reply_au16 ) / 2 ) );		
	}

}